<template>
  <view class='MessageCenter-container'>
    <MsgDialog ref="MsgDialog"></MsgDialog>
    <VIPExpiredDialog ref="VIPExpiredDialog"></VIPExpiredDialog>
    <ReportDialog ref="ReportDialog"></ReportDialog>
    <Pay ref="Pay" @success="onPaySuccess"></Pay>
    <LoginView ref="LoginView"></LoginView>
    <Share ref="Share"></Share>
  </view>
</template>

<script>
import {mapGetters} from 'vuex'
import msgUtils from "@/libs/msgUtils";
import MsgDialog from "@/components/MsgDialog.vue";
import ReportDialog from "@/components/ReportDialog.vue";
import Pay from "@/components/Pay.vue";
import VIPExpiredDialog from "@/components/VIPExpiredDialog.vue";
import LoginView from "@/components/LoginView/LoginView.vue";
import Share from "@/components/Share.vue";
import utils from "@/libs/utils";

export default {
  name: 'MessageCenter',
  components: {
    Pay,
    ReportDialog,
    VIPExpiredDialog,
    MsgDialog,
    LoginView,
    Share
  },
  computed: {
    ...mapGetters(['isLogined'])
  },
  data() {
    return {
      currentPage: null
    }
  },
  created() {
    this.currentPage = utils.getCurrentPage()
    console.log('监听：', this.currentPage)
  },

  mounted() {
    // this.onMessage({
    //   title: '您的VIP会员已到期',
    //   content: '过期时间：2023-03-18',
    //   href: '/pages/vip/vip',
    //   type: 'vip_expired',
    //   confirmText: '续费开通',
    //   cancelText: '不用了'
    // })
    // this.onMessage({type: 'tiyan'})

    this._handler = msgUtils.addListener((msg) => {
      if (msg.page && msg.page !== this.currentPage) {
        return false
      }

      if (msg.hidePage && msg.hidePage === this.currentPage) {
        return false
      }

      this.$nextTick(() => {
        this.onMessage(msg)
      })
      return true
    })
  },
  destroyed() {
    this._handler && this._handler()
  },
  methods: {
    open(msg) {
      const dialog = this.getDialog(msg.type)
      if (msg.type === 'share') {
        if (this.toLogin()) {
          return
        }
      }
      dialog.open(msg)
    },

    close(type) {
      const dialog = this.getDialog(type)
      dialog.close()
    },

    getDialog(type) {
      if (type === 'report') {
        return this.$refs.ReportDialog
      } else if (type === 'share') {
        return this.$refs.Share
      } else if (type === 'login') {
        return this.$refs.LoginView
      } else if (type === 'vip_expired') {
        return this.$refs.VIPExpiredDialog
      }
      return this.$refs.MsgDialog
    },

    onMessage(msg) {
      const dialog = this.getDialog(msg.type)
      dialog.open(msg)
    },
    toLogin() {
      if (!this.isLogined) {
        this.$refs.LoginView.open()
        return true
      }
    },
    toBuy(data) {
      if (this.toLogin()) {
        return
      }
      this.$refs.Pay.open(data)
    },
    onPaySuccess(result) {

    }
  }
}
</script>

<style scoped lang='scss'>
.MessageCenter-container {
}
</style>
